/**   
 * @Title: ResultActivity.java 
 * @Package: com.rename.icode.activity 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-3-6 上午9:52:18 
 * @version  
 */

package com.rename.icode.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.rename.icode.R;
import com.rename.icode.comm.Constants;
import com.rename.icode.utils.CommandUtil;

import java.util.List;

/**
 * @Description 显示运行结果
 * @author Administrator
 * @date 2015-3-6 上午9:52:18
 * @version V1.0
 */

public class ResultActivity extends ActivitySupport {

	private String mResult;

	private TextView mResultView;
	private View mTitleBackView;
	
	private static final int DELETE_EXE = 0;
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case DELETE_EXE:
				setResult(Activity.RESULT_OK);
				finish();
				break;

			default:
				break;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_result);

		init();

	}

	private void init() {
		getICodeApplication().addActivity(this);

		mResultView = (TextView) findViewById(R.id.result_txt);
		// 获取传递过来的参数
		Intent intent = getIntent();
		mResult = intent.getStringExtra("result");
		mResultView.setText(mResult);
		
		mTitleBackView = findViewById(R.id.result_title_back_btn);
		mTitleBackView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new Thread(){

					@Override
					public void run() {
						deleteExeFile();
						mHandler.sendEmptyMessage(DELETE_EXE);
					}
					
				}.start();
			}
		});
	}
	
	/**
	 * 
	 * @Description 删除编译后产生的可执行文件
	 * @author Administrator
	 * @return true 成功, false 失败
	 */
	private boolean deleteExeFile() {
		String[] commands = new String[] { "rm /data/" + Constants.EXE_FILE,
				"cd data", "ls" };
		List<String> results = CommandUtil.excuteCommand(commands);
		if (null == results) {// 命令执行后的结果有问题
			return false;
		}
		for (String result : results) {
			if (Constants.EXE_FILE.equals(result)) {// 文件仍然存在
				return false;
			}
		}
		return true;
	}

	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			deleteExeFile();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
